﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.commands;
using Microsoft.Win32;
using System.IO;
using InHD.utilities;
using InHD.dataaccess;

namespace InHD.viewmodels
{
    class InvoiceTplFormViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Constructor
        public InvoiceTplFormViewModel(InvoiceTemplate invoiceTemplate)
        {
            _invoiceTemplate = InvoiceTemplate.CreateNewInvoiceTemplate(invoiceTemplate.Id, invoiceTemplate.InvoiceUNK, invoiceTemplate.Content, invoiceTemplate.Name, invoiceTemplate.Modified, invoiceTemplate.ModifiedBy, invoiceTemplate.TransType, invoiceTemplate.Code, invoiceTemplate.RealContent, invoiceTemplate.ArchiveFlag);
            if (_invoiceTemplate.Id != 0) _invoiceTemplate.Content = "DBContent";
        }
        #endregion

        #region Value Property
        private InvoiceTemplate _invoiceTemplate;
        private InvoiceTemplateRepository _invoiceTplRepository = new InvoiceTemplateRepository();
        public string commandExcute = string.Empty;
        public RegisterPrintingRepository _regRepository = new RegisterPrintingRepository();
        bool CanDelete
        {
            get { return (this._invoiceTemplate.Id != 0  && string.IsNullOrEmpty(this._invoiceTemplate.ArchiveFlag))? true : false; }
        }

        #endregion

        #region Presentation Property

        public InvoiceTemplate InvoiceTemplate
        {
            get
            {
                return _invoiceTemplate;
            }
        }

        public string Name
        {
            get
            {
                return _invoiceTemplate.Name;
            }
            set
            {
                if (_invoiceTemplate.Name != value)
                {
                    _invoiceTemplate.Name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public string Content
        {
            get
            {
                return _invoiceTemplate.Content;
            }
            set
            {
                if (_invoiceTemplate.Content != value)
                {
                    _invoiceTemplate.Content = value;
                    OnPropertyChanged("Content");
                }
            }
        }

        public TransTypes TransType
        {
            get
            {
                return _invoiceTemplate.TransType;
            }
            set
            {
                if (_invoiceTemplate.TransType != value)
                {
                    _invoiceTemplate.TransType = value;
                    OnPropertyChanged("TransType");
                }
            }
        }

        public string Code
        {
            get
            {
                return _invoiceTemplate.Code;
            }
            set
            {
                if (_invoiceTemplate.Code != value)
                {
                    _invoiceTemplate.Code = value;
                    OnPropertyChanged("Code");
                }
            }
        }

        public bool HasContent
        {
            get
            {
                return _invoiceTemplate.Id != 0;
            }
        }

        public bool HasntContent
        {
            get
            {
                return _invoiceTemplate.Id == 0;
            }
        }

        public string ArchiveFlag
        {
            get
            {
                return _invoiceTemplate.ArchiveFlag;
            }
        }

        #endregion

        #region IDataErrorInfo Interface
        string IDataErrorInfo.Error
        {
            get { return (_invoiceTemplate as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                error = (_invoiceTemplate as IDataErrorInfo)[propertyName];


                // Dirty the commands registered with CommandManager,
                // such as our Save command, so that they are queried
                // to see if they can execute now.
                CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }
        #endregion

        #region Command
        private RelayCommand _closeCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        (param) => this.Close());
                }
                return _closeCommand;
            }
        }

        private RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(), (param) => this._invoiceTemplate.IsValid);
                }
                return _saveCommand;
            }
        }

        private RelayCommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new RelayCommand(
                        (param) => this.Delete(), (param)=>this.CanDelete);
                }
                return _deleteCommand;
            }
        }

        private RelayCommand _browseFileCommand;
        public ICommand BrowseFileCommand
        {
            get
            {
                if (_browseFileCommand == null)
                {
                    _browseFileCommand = new RelayCommand(
                        (param) => this.BrowseFile());
                }
                return _browseFileCommand;
            }
        }

        private RelayCommand _viewContentFile;
        public ICommand ViewContentFileCommand
        {
            get
            {
                if (_viewContentFile == null)
                {
                    _viewContentFile = new RelayCommand(
                        (param) => this.ViewContentFile());
                }
                return _viewContentFile;
            }
        }

        #endregion

        #region Command Method
        void Close()
        {
            commandExcute = "Close";
            OnRequestClose();
        }

        void Save()
        {
            if (_invoiceTemplate.Id == 0)
            {
                InHD.dataaccess.InvoiceTemplateRepository.AddNewInvoiceTemplate(_invoiceTemplate);
            }
            else
            {
                if (_regRepository.CheckRegisterExistenceByInvoiceTpl(_invoiceTemplate.InvoiceUNK) == null)
                {
                    _invoiceTplRepository.EditAnInvoiceTemplate(_invoiceTemplate);
                }
                else
                {
                    System.Windows.MessageBox.Show("Can not edit this Template because of Associated Registration");
                }
            }

            commandExcute = "Save";
            OnRequestClose();
        }

        void Delete()
        {
            if (_regRepository.CheckRegisterExistenceByInvoiceTpl(_invoiceTemplate.InvoiceUNK)!=null)
            {
                _invoiceTplRepository.DeleteAnInvoiceTemplate(_invoiceTemplate.InvoiceUNK);
            }
            else
            {
                System.Windows.MessageBox.Show("Can not delete this Template because of Associated Registration");
            }

            commandExcute = "Delete";
            OnRequestClose();
        }

        void BrowseFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "inhd template (*.xaml)|*.xaml";
            if (openFileDialog.ShowDialog() == true) {
                this.Content = openFileDialog.FileName;
                OnPropertyChanged("Content");
            }
        }

        void ViewContentFile()
        {
            InvoicePrinter printer = new InvoicePrinter(_invoiceTemplate.RealContent);
            printer.ShowPrintPreview();
        }

        #endregion

        #region Normal Method
        #endregion
    }
}
